package business;

import org.apache.log4j.Logger;
import java.util.Collection;
import java.util.HashMap;

public class BasicBank implements Bank {
    // default size of the accounts collection
    private final int ACCOUNT_COLLECTION_INITIAL_CAPACITY = 32;
    // default size of the cashier collection
    private final int CASHIER_COLLECTION_INITIAL_CAPACITY = 16;
    // collection of accounts
    private final HashMap<Integer, Account> accounts;
    // collection of cashiers
    private final HashMap<Integer, Cashier> cashiers;
    // logger
    private static final Logger LOGGER = Logger.getLogger(BasicBank.class);


    // constructors
    public BasicBank() {
        accounts = new HashMap<Integer, Account>(ACCOUNT_COLLECTION_INITIAL_CAPACITY);
        cashiers = new HashMap<Integer, Cashier>(CASHIER_COLLECTION_INITIAL_CAPACITY);
    }

    @Override
    public Collection<Cashier> getCashiers() {
        return cashiers.values();
    }

    @Override
    public Collection<Account> getAccounts() {
        return accounts.values();
    }

    @Override
    public int getCashiersCount() {
        return cashiers.size();
    }

    @Override
    public boolean hasAccount(int key) {
        return accounts.containsKey(key);
    }

    @Override
    public Account getAccount(int key) {
        return accounts.get(key);
    }

    @Override
    public Account createAccount(int key) {
        Account account = new BasicAccount(key);
        accounts.put(key, account);
        return account;
    }

    public Account createAccount(int key, double money) {
        Account account = new BasicAccount(key, money);
        accounts.put(key, account);
        return account;
    }

    @Override
    public Cashier getCashier(int key) {
        return cashiers.get(key);
    }

    @Override
    public Cashier createCashier(int key) {
        Cashier cashier = new BankCashier(key, this);
        cashiers.put(key, cashier);
        return cashier;
    }

    @Override
    public int getAccountsCount() {
        return accounts.size();
    }

    @Override
    public void closeAccount(int key) {
        accounts.remove(key);
    }

    @Override
    public void turnCashierOut(int key) {
        cashiers.remove(key);
    }

    @Override
    public void setAsBankrupt() {
        accounts.clear();
    }

    @Override
    public void turnAllCashiersOut() {
        cashiers.clear();
    }
}
